package com.jams.goodox.utils;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;
import android.text.style.RelativeSizeSpan;

import java.text.DecimalFormat;

/**
 * Created by jams on 2017/5/11.
 * ***
 * Tel: ***
 */

public class PriceUtils {
    /**
     * 显示不同大小的字符串
     *
     * @param unit  單位
     * @param money 价格
     * @return spannable
     */
    public static SpannableStringBuilder getPriceStyle(Double money, String unit) {
        String price = "";
        SpannableStringBuilder spannable;
        price = getPriceString(money);
        String priceStr = "¥" + price + unit;
        spannable = new SpannableStringBuilder(priceStr);//用于可变字符串
        CharacterStyle span_1 = new RelativeSizeSpan(1.3f);
        spannable.setSpan(span_1, 1, String.valueOf(money.intValue()).length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannable;
    }

    //新的获取价格方法
    public static String getPriceString(Double price) {
        if (price == null) {
            return "0";
        }
        if ((int) price.doubleValue() == price) {
            return String.valueOf((int) price.doubleValue());
        }
        DecimalFormat df = new DecimalFormat("######0.00");
        return df.format(price);
    }
}
